package com.codedrunks.components.containers
{
	import com.codedrunks.components.event.ExtendedAccordionEvent;	
	
	import flexlib.containers.WindowShade;	
	
	import mx.events.FlexEvent;	
	
	import flash.events.Event;	
	import flash.events.TimerEvent;	
	import flash.utils.Timer;
	
	
	import mx.core.EdgeMetrics;	

	import flexlib.events.WindowShadeEvent;	
	import mx.core.Container;
	import mx.containers.VBox;	
	import flash.display.DisplayObject;
	import flash.display.BitmapData; 	
	
	public class ExtendedAccordion extends VBox
	{
		private var timer:Timer;
		private var childsList:Array = new Array();
		public var collapsedIndexes:Array = new Array();
		
		public function ExtendedAccordion()
		{
			super();
			this.setStyle('verticalGap', 0);
		}
		
		/*
		 * overriding the initialize method for layouting
		override public function initialize():void
		{
			super.initialize();
		}
		 */
	
		
		/*
		 * overriding addChild
		 */
		override public function addChild(child:DisplayObject):DisplayObject
		{
			childsList.push(child);
			var windowShade:WindowShade = new WindowShade();
			windowShade.percentWidth = 100;
			windowShade.height = child.height + windowShade.borderMetrics.top;
			windowShade.label = Container(child).label;
			windowShade.setStyle("paddingTop", 0);
			windowShade.addEventListener(WindowShadeEvent.OPEN_END, handleResizeEndEvent);
			windowShade.addEventListener(WindowShadeEvent.CLOSE_END, handleResizeEndEvent);
			windowShade.addEventListener(WindowShadeEvent.OPEN_BEGIN, handleResizeBeginEvent);
			windowShade.addEventListener(WindowShadeEvent.CLOSE_BEGIN, handleResizeBeginEvent);
			windowShade.addEventListener(FlexEvent.CREATION_COMPLETE, handleWindowCreationComplete);
			windowShade.addChild(child);
			super.addChild(windowShade);
			return child;
			
		}
		
		/*
		 * comment
		 */
		private function handleWindowCreationComplete(event:FlexEvent):void
		{
			for(var i:int = 0; i<collapsedIndexes.length; i++)
			{
				var collapsedIndex:int = collapsedIndexes[i];
				if(collapsedIndexes.length > 0 && this.getChildren().length > collapsedIndex)
				{
					if(event.target == this.getChildAt(collapsedIndexes[i]))
					{
						WindowShade(event.target).opened = false;
					}
					else
					{
						WindowShade(event.target).opened = true;
						return;
					}
				}
			}
		}
		
		/*
		 * method to handle the open end event
		 */
		private function handleResizeEndEvent(event:WindowShadeEvent):void
		{
			if(timer != null) 
			{
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, resizeWindow);
			}
			resizeWindow();
		}
		
		
		/*
		 * method to handle the open end event
		 */
		private function handleResizeBeginEvent(event:WindowShadeEvent):void
		{
			if(timer != null)
			{
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, resizeWindow);
			}
			timer = new Timer(10);
			timer.addEventListener(TimerEvent.TIMER, resizeWindow);
			timer.start();
		}
		
		/*
		 * methode to resize window
		 */
		private function resizeWindow(event:TimerEvent = null):void
		{
			var evt:ExtendedAccordionEvent = new ExtendedAccordionEvent(ExtendedAccordionEvent.RESIZE_CONTENT);
			evt.newWidth = this.width;
			evt.newHeight = this.height;
			this.dispatchEvent(evt);
		}
		
	}
}